package com.group.three.controller;

import com.github.pagehelper.PageInfo;
import com.group.three.pojo.FavoriteVideos;
import com.group.three.pojo.Person;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.FavoriteVideosService;
import com.group.three.utils.SnowFlakeUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class FavoriteVideosController {
    @Autowired
    private FavoriteVideosService favoriteVideosService;

    @RequestMapping("/move/favorite")
    public ModelAndView mav(ModelAndView modelAndView) {
        List<FavoriteVideos> favoriteVideos = favoriteVideosService.selectFavoriteAll();
        PageInfo pageInfo = new PageInfo(favoriteVideos);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("3-admin/moviecol_list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/move/DeleteById")
    public JsonResult favorite(Long favoiteId) {
        int deleteResult = favoriteVideosService.deleteFavoriteVideosId(favoiteId);
        if (deleteResult <= 0) {
            JsonResult jsonResult = new JsonResult("0", "失败");
            return jsonResult;
        }
        JsonResult jsonResult = new JsonResult("200", "成功");
        return jsonResult;
    }

    @RequestMapping(value = "/favoritePage")
    public ModelAndView favoritePage(Map<String, Object> map, HttpSession session, ModelAndView modelAndView,
                                     @RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "5") int size) {
        List<FavoriteVideos> list = favoriteVideosService.selectAll(page, size);
        PageInfo pageInfo = new PageInfo(list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("3-admin/moviecol_list");
        return modelAndView;
    }

    @ResponseBody

    @RequestMapping("/favoriteSearch")
    public JsonResult searchByInput(String input) {

        List<FavoriteVideos> favoriteVideos = favoriteVideosService.searchByInput(input);

        return new JsonResult("200", "success", favoriteVideos);
    }

    @RequestMapping("/selectFavorite")
    public ModelAndView selectFavorite(ModelAndView modelAndView) {
        List<FavoriteVideos> favoriteVideos = favoriteVideosService.selectFavoriteAll();
        modelAndView.addObject("favoriteVideos", favoriteVideos);
        modelAndView.setViewName("2-movie/play");
        return modelAndView;
    }


    @RequestMapping("/insertVideoS")
    @ResponseBody
    public JsonResult insertVideoS(
            @RequestParam(value = "videoNumber", required = false) String videoNumber,
            @RequestParam(value = "personId", required = false) String personId,
            @RequestParam(value = "favoriteState", required = false) Integer favoriteState
    ) {
        boolean a = favoriteVideosService.selectIsFavorite(Long.parseLong(videoNumber), Long.parseLong(personId), 1);
        boolean b = favoriteVideosService.selectIsFavorite(Long.parseLong(videoNumber), Long.parseLong(personId), 2);
//没有数据为他true
        if (a && b) {
            return new JsonResult("200", "没有数据", false);

        } else if (!a) {
            return new JsonResult("1", "收藏了", true);
        } else if (!b) {
            return new JsonResult("2", "收藏过", false);
        }else {
            return new JsonResult("400","ERROR");
        }

    }


    //取消收藏
    @RequestMapping("/cancal")
    @ResponseBody
    public JsonResult cancal(
            @RequestParam(value = "personId") String personId,
            @RequestParam(value = "videoNumber") String videoNumber
    ) {
        int i = favoriteVideosService.deleteCancel(Long.parseLong(personId), Long.parseLong(videoNumber));
        if (i != 1) {
            return new JsonResult("400", "fail");
        } else {
            return new JsonResult("200", "success");
        }

    }

    //收藏
    @RequestMapping("/collection")
    @ResponseBody
    public JsonResult collection(
            @RequestParam(value = "videoNumber", required = false) String videoNumber,
            @RequestParam(value = "personId", required = false) String personId,
            @RequestParam(value = "collectionTime", required = false) String collectionTime
    ) {
        boolean b = favoriteVideosService.selectIsFavorite(Long.parseLong(videoNumber), Long.parseLong(personId), 2);
        //如果没有true
        if (b) {
            int i = favoriteVideosService.insertVideo(SnowFlakeUtil.getSnowFlakeId(), Long.parseLong(videoNumber), Long.parseLong(personId), collectionTime, 1);
            if (i != 1) {
                return new JsonResult("400", "fail");
            } else {
                return new JsonResult("200", "success");
            }
        } else {
            int i = favoriteVideosService.deleteCancel(Long.parseLong(videoNumber), Long.parseLong(personId));
            if (i != 0) {
                return new JsonResult("200", "success");
            } else {
                return new JsonResult("400", "fail");
            }
        }

    }


    @RequestMapping("/adminFavoriteVideosId/{personId}")
    public ModelAndView adminFavoriteVideosId(ModelAndView modelAndView,
                                              @PathVariable(value = "personId") Long personId) {
        List<FavoriteVideos> favoriteVideos = favoriteVideosService.selectAdminFavoriteVideosId(personId);
        modelAndView.addObject("favoriteVideos", favoriteVideos);
        modelAndView.setViewName("/2-movie/moviecol");
        return modelAndView;
    }
}
